package cn.shujuhai.common.mapper;


import cn.shujuhai.common.domain.SysUser;
import cn.shujuhai.common.service.DepartmentService;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author NiuYahu
 * @description 针对表【sys_user(用户表)】的数据库操作Mapper
 * @createDate 2024-09-25 16:58:56
 * @Entity cn.shujuhai.domain.SysUser
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {

    @Select("SELECT sp.permission_code " +
            "FROM sys_user su " +
            "JOIN sys_user_role sur ON su.user_id = sur.user_id " +
            "JOIN sys_role sr ON sur.role_id = sr.role_id " +
            "JOIN sys_role_permission srp ON sr.role_id = srp.role_id " +
            "JOIN sys_permission sp ON srp.permission_id = sp.permission_id " +
            "WHERE su.user_id = #{userId} AND sur.deleted <> 1  AND srp.deleted <> 1 ")
    List<String> selectPermissionCodesByUserId(@Param("userId") Long userId);

    @Select("SELECT sr.role_code " +
            "FROM sys_user su " +
            "JOIN sys_user_role sur ON su.user_id = sur.user_id " +
            "JOIN sys_role sr ON sur.role_id = sr.role_id " +
            "WHERE su.user_id = #{userId}  AND sur.deleted <> 1 ")
    List<String> selectRoleCodesByUserId(@Param("userId") Long userId);

    @Select("SELECT spm.menu_id " +
            "FROM sys_user su " +
            "JOIN sys_user_role sur ON su.user_id = sur.user_id " +
            "JOIN sys_role sr ON sur.role_id = sr.role_id " +
            "JOIN sys_role_permission srp ON sr.role_id = srp.role_id " +
            "JOIN sys_permission sp ON srp.permission_id = sp.permission_id " +
            "JOIN sys_permission_menu spm ON sp.permission_id = spm.permission_id " +
            "WHERE su.user_id = #{userId} AND sur.deleted <> 1 AND srp.deleted <> 1 AND spm.deleted <> 1")
    List<Long> getUserMenuIds(@Param("userId") Long userId);

    @Select("SELECT spm.menu_id " +
            "FROM sys_user su " +
            "JOIN role_department rd ON su.user_id = rd.user_id " +
            "JOIN sys_role sr ON rd.role_id = sr.role_id " +
            "JOIN sys_role_permission srp ON sr.role_id = srp.role_id " +
            "JOIN sys_permission sp ON srp.permission_id = sp.permission_id " +
            "JOIN sys_permission_menu spm ON sp.permission_id = spm.permission_id " +
            "WHERE su.user_id = #{userId} " +
            "AND rd.department_id = #{departmentId} " +
            "AND rd.deleted <> 1 " +
            "AND srp.deleted <> 1 " +
            "AND spm.deleted <> 1")
    List<Long> getUserMenuIdsByUserIdAndDeptId(@Param("userId") Long userId, @Param("departmentId") Long departmentId);

    @Select("SELECT sc.channel_id " +
            "FROM sys_user su " +
            "JOIN sys_user_role sur ON su.user_id = sur.user_id " +
            "JOIN sys_role sr ON sur.role_id = sr.role_id " +
            "JOIN sys_role_channel sc ON sr.role_id = sc.role_id " +
            "WHERE su.user_id = #{userId} AND sur.deleted <> 1 AND sc.deleted <> 1")
    List<Long> selectChannelIdByUserId(@Param("userId") Long userId);

    @Select("SELECT sc.info_id " +
            "FROM sys_user su " +
            "JOIN sys_user_role sur ON su.user_id = sur.user_id " +
            "JOIN sys_role sr ON sur.role_id = sr.role_id " +
            "JOIN sys_role_info sc ON sr.role_id = sc.role_id " +
            "WHERE su.user_id = #{userId} AND sur.deleted <> 1 AND sc.deleted <> 1"
    )
    List<Long> selectInfoIdByUserId(@Param("userId") Long userId);


}




